#!/usr/bin/env python
# encoding: utf-8
"""
admin.py

Created by Mohammad Khatib on 2008-11-07.
Copyright (c) 2008 __MyCompanyName__. All rights reserved.
"""

import sys
import os
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import users

from helpal_models import HelpalUser
from helpal_models import Question



class NewQuestionHandler(webapp.RequestHandler):
	def get(self):
		self.response.out.write("""
				<h1>Ask New Question</h1>
				<form method='post'>
					<table>
						<tr>
							<td>Title: </td>
							<td><input type='text' name='title' size='46'/></td>
						</tr>
						<tr>
							<td>Description: </td>
							<td><textarea name='description' cols='40' rows='5'></textarea></td>
						</tr>
						<tr>
							<td colspan='2'><input type='submit' name='Ask' value='Ask' /></td>
						</tr>
					</table>
				</form>
		""")
	def post(self):
		# Saving Question in Datastore
		admin = HelpalUser(user=users.get_current_user())
		admin.put()
		_title = self.request.get('title')
		_description = self.request.get('description')
		question = Question(title=_title,description=_description,author=admin)
		question.put()
		self.redirect('/')
		


def main():
	application = webapp.WSGIApplication([('/admin/Ask', NewQuestionHandler)], debug=True)
	wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
	main()

